package cn.community.care.module.system.service.hotproducts;

import cn.community.care.module.system.controller.admin.hotproducts.vo.HotProductsPageReqVO;
import cn.community.care.module.system.controller.admin.hotproducts.vo.HotProductsSaveReqVO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.community.care.module.system.dal.dataobject.hotproducts.HotProductsDO;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.util.object.BeanUtils;

import cn.community.care.module.system.dal.mysql.hotproducts.HotProductsMapper;

import javax.annotation.Resource;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 热门商品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HotProductsServiceImpl implements HotProductsService {

    @Resource
    private HotProductsMapper hotProductsMapper;

    @Override
    public Long createHotProducts(HotProductsSaveReqVO createReqVO) {
        // 插入
        HotProductsDO hotProducts = BeanUtils.toBean(createReqVO, HotProductsDO.class);
        hotProductsMapper.insert(hotProducts);
        // 返回
        return hotProducts.getId();
    }

    @Override
    public void updateHotProducts(HotProductsSaveReqVO updateReqVO) {
        // 校验存在
        validateHotProductsExists(updateReqVO.getId());
        // 更新
        HotProductsDO updateObj = BeanUtils.toBean(updateReqVO, HotProductsDO.class);
        hotProductsMapper.updateById(updateObj);
    }

    @Override
    public void deleteHotProducts(Long id) {
        // 校验存在
        validateHotProductsExists(id);
        // 删除
        hotProductsMapper.deleteById(id);
    }

    private void validateHotProductsExists(Long id) {
        if (hotProductsMapper.selectById(id) == null) {
//            throw exception(HOT_PRODUCTS_NOT_EXISTS);
        }
    }

    @Override
    public HotProductsDO getHotProducts(Long id) {
        return hotProductsMapper.selectById(id);
    }

    @Override
    public PageResult<HotProductsDO> getHotProductsPage(HotProductsPageReqVO pageReqVO) {
        return hotProductsMapper.selectPage(pageReqVO);
    }

    public List<Map<String, Object>> countTotalPriceByDayInLast7Days() {
        return hotProductsMapper.countTotalPriceByDayInLast7Days();
    }

}